/**
 * Z-score评分计算测试
 * 用于验证评分算法的正确性
 */

/**
 * 测试数据集
 */
const testData = {
  // 不良贷款率（负向指标）
  badLoanRate: {
    name: '不良贷款率',
    isPositive: false,
    values: [0.50, 0.80, 1.00, 1.25, 1.50, 1.80, 2.10]
  },
  // 存款余额增速（正向指标）
  depositGrowth: {
    name: '存款余额增速',
    isPositive: true,
    values: [5.5, 8.0, 10.0, 12.0, 15.5, 16.0, 18.2]
  }
}

/**
 * 计算平均值
 */
function calculateMean(values) {
  const sum = values.reduce((acc, val) => acc + val, 0)
  return sum / values.length
}

/**
 * 计算标准差
 */
function calculateStdDev(values, mean) {
  const squaredDiffs = values.map(val => Math.pow(val - mean, 2))
  const variance = squaredDiffs.reduce((acc, val) => acc + val, 0) / values.length
  return Math.sqrt(variance)
}

/**
 * 计算Z-score
 */
function calculateZScore(value, mean, stdDev) {
  if (stdDev === 0) return 0
  return (value - mean) / stdDev
}

/**
 * Z-score转换为0-100分
 */
function zScoreToScore(zScore, isPositive) {
  let limitedZScore = Math.max(-3, Math.min(3, zScore))
  if (!isPositive) {
    limitedZScore = -limitedZScore
  }
  const score = ((limitedZScore + 3) / 6) * 100
  return Math.round(score * 100) / 100
}

/**
 * 运行测试
 */
function runTest() {
  console.log('=' .repeat(80))
  console.log('Z-score 评分计算测试')
  console.log('=' .repeat(80))
  console.log('')

  Object.entries(testData).forEach(([key, config]) => {
    console.log(`📊 ${config.name}（${config.isPositive ? '正向指标 ↑' : '负向指标 ↓'}）`)
    console.log('-'.repeat(80))
    
    // 计算统计信息
    const mean = calculateMean(config.values)
    const stdDev = calculateStdDev(config.values, mean)
    
    console.log(`样本数: ${config.values.length}`)
    console.log(`平均值: ${mean.toFixed(2)}`)
    console.log(`标准差: ${stdDev.toFixed(2)}`)
    console.log(`最小值: ${Math.min(...config.values)}`)
    console.log(`最大值: ${Math.max(...config.values)}`)
    console.log('')
    
    // 计算每个值的得分
    console.log('原始值 | Z-score | 得分 | 评价')
    console.log('-'.repeat(80))
    
    config.values.forEach(value => {
      const zScore = calculateZScore(value, mean, stdDev)
      const score = zScoreToScore(zScore, config.isPositive)
      
      let rating = ''
      if (score >= 80) rating = '优秀 🎉'
      else if (score >= 60) rating = '良好 👍'
      else if (score >= 40) rating = '一般 😐'
      else rating = '较差 😟'
      
      console.log(`${value.toFixed(2).padEnd(7)} | ${zScore.toFixed(2).padStart(7)} | ${score.toFixed(2).padStart(5)} | ${rating}`)
    })
    
    console.log('')
    console.log('')
  })

  // 验证关键点
  console.log('=' .repeat(80))
  console.log('✅ 验证关键点')
  console.log('=' .repeat(80))
  console.log('')
  
  // 验证1：平均值应该得50分
  const testValues = [10, 20, 30, 40, 50]
  const testMean = calculateMean(testValues)
  const testStdDev = calculateStdDev(testValues, testMean)
  const avgZScore = calculateZScore(testMean, testMean, testStdDev)
  const avgScore = zScoreToScore(avgZScore, true)
  
  console.log(`✓ 测试1：平均值得分验证`)
  console.log(`  数据: [${testValues.join(', ')}]`)
  console.log(`  平均值: ${testMean}`)
  console.log(`  Z-score: ${avgZScore.toFixed(2)}（应该=0）`)
  console.log(`  得分: ${avgScore.toFixed(2)}（应该=50）`)
  console.log(`  结果: ${avgScore === 50 ? '✅ 通过' : '❌ 失败'}`)
  console.log('')
  
  // 验证2：正向指标高于平均值应该>50分
  const highValue = 60
  const highZScore = calculateZScore(highValue, testMean, testStdDev)
  const highScore = zScoreToScore(highZScore, true)
  
  console.log(`✓ 测试2：正向指标高于平均值`)
  console.log(`  数值: ${highValue}（高于平均值${testMean}）`)
  console.log(`  Z-score: ${highZScore.toFixed(2)}（应该>0）`)
  console.log(`  得分: ${highScore.toFixed(2)}（应该>50）`)
  console.log(`  结果: ${highScore > 50 ? '✅ 通过' : '❌ 失败'}`)
  console.log('')
  
  // 验证3：负向指标高于平均值应该<50分
  const highValueNeg = 60
  const highZScoreNeg = calculateZScore(highValueNeg, testMean, testStdDev)
  const highScoreNeg = zScoreToScore(highZScoreNeg, false)
  
  console.log(`✓ 测试3：负向指标高于平均值`)
  console.log(`  数值: ${highValueNeg}（高于平均值${testMean}）`)
  console.log(`  Z-score: ${highZScoreNeg.toFixed(2)}（应该>0）`)
  console.log(`  得分: ${highScoreNeg.toFixed(2)}（应该<50，因为是负向）`)
  console.log(`  结果: ${highScoreNeg < 50 ? '✅ 通过' : '❌ 失败'}`)
  console.log('')
  
  console.log('=' .repeat(80))
  console.log('✅ 测试完成！')
  console.log('=' .repeat(80))
}

// 导出测试函数
export { runTest }

// 如果直接运行（非模块导入）
if (typeof window !== 'undefined') {
  window.runScoreTest = runTest
}















